ELF := hack.elf
BIN := hack.bin

SRCS := hack.c

S_SRCS := hackf.S

ifdef MSLUG5
$(warning  $(XXX))
HACK_VERSION := MSLUG5
JMP := jmp5.S
BIN_ADDRESS := 0x0FC000
JMP_ADDRESS := 0x49b3A
HACK_LDS := hack5.ld
else
ifdef MSLUG4
#暂时先用0xcd0a0  0xff 区域
HACK_VERSION := MSLUG4
BIN_ADDRESS := 0xcd0a0
JMP_ADDRESS := 0x8D92
HACK_LDS := hack4.ld
JMP := jmp4.S
else
ifdef MSLUG3
HACK_VERSION := MSLUG3
JMP := jmp3.S
BIN_ADDRESS := 0x0FA300
JMP_ADDRESS := 0x28A44
HACK_LDS := hack3.ld
NOSWAP = -noswp
else
ifdef MSLUG2
HACK_VERSION := MSLUG2
BIN_ADDRESS := 0xD8000
JMP_ADDRESS := 0x9F12A
HACK_LDS := hack2.ld
JMP := jmp2.S
else
ifdef MSLUG1
HACK_VERSION := MSLUG1
BIN_ADDRESS := 0xF5000
JMP_ADDRESS := 0x5CC70
WBIN_ADDRESS := 0x1F5000
WJMP_ADDRESS := 0x15CC70
JMP := jmp1.S
HACK_LDS := hack1.ld
else
JMP := jmp.S

HACK_VERSION := MSLUGX
#jmp写入地址
JMP_ADDRESS := 0xCA24C
BIN_ADDRESS := 0xFE000
HACK_LDS := hack.ld
endif
endif
endif
endif
endif

JMP_BIN := jmp.bin

OBJS :=  \
	$(S_SRCS:.S=.o) \
	$(SRCS:.c=.o)

ARCHPATH :=	E:/programfiles/SysGCC/bin/
CC 			:= $(ARCHPATH)m68k-elf-gcc.exe
OBJCOPY		:= $(ARCHPATH)m68k-elf-objcopy

.PHONY:write clean \
	write5 echo \
	write5_bin\
	write4_bin\
	write3_bin\
	write2_bin\
	write1_bin

write:$(JMP_BIN) $(BIN)
	luajit writefile.lua 250-p1.p1 jmp.bin $(JMP_ADDRESS)
	luajit writefile.lua 250-p1.p1 hack.bin $(BIN_ADDRESS)
	cp 250-p1.p1 ../../mslugx/

write5_bin:$(JMP_BIN) $(BIN)
	luajit writefile.lua mslug5.bin jmp.bin $(JMP_ADDRESS)
	luajit writefile.lua mslug5.bin hack.bin $(BIN_ADDRESS)
	luajit mslug5en.lua mslug5.bin
	luajit progbk3s.lua mslug5.bin.d 268-p1cr.p1 268-p2cr.p2
	cp	268-p1cr.p1 ../../mslug5/
	cp  268-p2cr.p2 ../../mslug5/

write3_bin:$(JMP_BIN) $(BIN)
	luajit writefile.lua mslug3.bin hack.bin $(BIN_ADDRESS) $(NOSWAP)
	luajit writefile.lua mslug3.bin jmp.bin $(JMP_ADDRESS) $(NOSWAP)
	luajit mslug3en.lua mslug3.bin
	cp mslug3.bin.d1 ../../mslug3/256-pg1.p1
	cp mslug3.bin.d2 ../../mslug3/256-pg2.p2
	cp mslug3.bin.d3 ../../mslug3/neo-sma

write4_bin:$(JMP_BIN) $(BIN)
	luajit writefile.lua 263-p1.p1 hack.bin $(BIN_ADDRESS) $(NOSWAP)
	luajit writefile.lua 263-p1.p1  jmp.bin $(JMP_ADDRESS) $(NOSWAP)
	cp 263-p1.p1 ../../mslug4/

write2_bin:$(JMP_BIN) $(BIN)
	luajit writefile.lua 241-p1.p1 hack.bin $(BIN_ADDRESS) $(NOSWAP)
	luajit writefile.lua 241-p1.p1  jmp.bin $(JMP_ADDRESS) $(NOSWAP)
	cp 241-p1.p1 ../../mslug2/


write1_bin:$(JMP_BIN) $(BIN)
	luajit writefile.lua 201-p1.p1 hack.bin $(WBIN_ADDRESS) $(NOSWAP)
	luajit writefile.lua 201-p1.p1  jmp.bin $(WJMP_ADDRESS) $(NOSWAP)
	cp 201-p1.p1 ../../mslug/

echo:
	echo hack $(HACK_VERSION)

write5:
	make -C . write5_bin MSLUG5=1

write4:
	make -C . write4_bin MSLUG4=1

write3:
	make -C . write3_bin MSLUG3=1

write2:
	make -C . write2_bin MSLUG2=1

write1:
	make -C . write1_bin MSLUG1=1


$(BIN):$(ELF)
	$(OBJCOPY) -O binary $(<)  $(@)

$(JMP_BIN):$(JMP:.S=.o)
	$(OBJCOPY) -O binary $(<)  $(@)

$(ELF):$(OBJS)
	$(CC) $(^) -o $(@) -nostdlib -nodefaultlibs -T$(HACK_LDS)

%.o:%.S
	$(CC) -c -o $(@) $(<) -D$(HACK_VERSION) -m68000 -D$(HACK_VERSION) -DBIN_ADDRESS=$(BIN_ADDRESS)

%.o:%.c
	$(CC) -c -o $(@) $(<) -O2 -m68000 -D$(HACK_VERSION) -fomit-frame-pointer

clean:
	-rm $(OBJS) $(JMP_BIN)

